﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.AutoCutBoard;
using Leadtools.Codecs;
using Leadtools;
using MINERP.MODEL.FoundationModel;

namespace MINERP.AutoCutBoard
{
    public partial class FrmMain : Form
    {
        CutBoardBLL _bll = new CutBoardBLL();
        Board_Info boardInfo;
        Printer_Info printerInfo;
        public int CalibrationPrinterId =0;
        public int lastBoardId = 0;
        //StringBuilder enterChar = new StringBuilder();//存储输入字符
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            int ret;
            btnQuery.Enabled = false;
            Cursor = Cursors.WaitCursor;
            try
            {
                if (string.IsNullOrEmpty(txtBoardNo.Text))
                {
                    MessageBox.Show("请输入大版号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (int.TryParse(txtBoardNo.Text, out ret))
                {
                    boardInfo = _bll.GetBoardInfo(Convert.ToInt32(txtBoardNo.Text));
                    if (boardInfo != null)
                    {
                        printerInfo=_bll.GetPrinterInfo(boardInfo.PrinterId);
                        SetBigImage(boardInfo.PicPath);
                    }
                    else
                    {
                        MessageBox.Show("大版不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的大版号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                btnQuery.Enabled = true;
                Cursor = Cursors.Default;
                txtBoardNo.Focus();

            }

            

        }

        private void SetBigImage(string picPath)
        {
            try
            {
                RasterCodecs.Startup();
                RasterCodecs codecs = new RasterCodecs();
                RasterImage img = codecs.Load(picPath);
                rasterImg.SizeMode = RasterPaintSizeMode.FitAlways;
                rasterImg.Image = img;
                RasterCodecs.Shutdown();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            bool isShowConfirmDialog = false;//是否显示裁切窗口
            isShowConfirmDialog = chkShowdialog.Checked;
            string ret;
            btnCut.Enabled = false;
            
            try
            {
                if (_bll.isMachineBusy())
                {
                    MessageBox.Show("裁切任务正在进行中，请稍等。。。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (boardInfo == null)
                {
                    try
                    {
                        boardInfo = _bll.GetBoardInfo(Convert.ToInt32(txtBoardNo.Text));
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        txtBoardNo.Clear();
                    }

                    if (boardInfo != null)
                    {
                        if (boardInfo.BoardId == lastBoardId)
                        {
                            if (DialogResult.OK != MessageBox.Show("该大版已经裁切过，确定再次裁切?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                            {
                                return;
                            }
                        }
                        lastBoardId = boardInfo.BoardId;
                        tsstxtBoardInfo.Text = string.Format("当前裁切的大版号：{0}，路径：{1}", boardInfo.BoardId, boardInfo.PicPath);
                        if (!string.IsNullOrEmpty(boardInfo.AutoPoint))
                        {
                            if (printerInfo == null)
                            {
                                printerInfo = _bll.GetPrinterInfo(boardInfo.PrinterId);
                            }
                            if (printerInfo != null)
                            {
                                if (!string.IsNullOrEmpty(printerInfo.CalibrationData))
                                {
                                    ret = _bll.VectorPointToMachine(boardInfo.AutoPoint, printerInfo.CalibrationData, cmbxComPort.SelectedIndex + 1, isShowConfirmDialog);
                                    if (!string.IsNullOrEmpty(ret))
                                    {
                                        MessageBox.Show(ret, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("请校准: " + printerInfo.PName + " ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                            }
                            else
                            {
                                MessageBox.Show("没找到大版的打印信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }

                        else
                        {
                            MessageBox.Show("该大版没有生成裁切坐标信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                  
                    }
                    else
                    {

                        MessageBox.Show("没找到大版，请点击“查询”确定该大版是否存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                btnCut.Enabled = true;
                boardInfo = null;
                printerInfo = null;
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            cmbxComPort.SelectedIndex = 0;
            if (Properties.Settings.Default.COMPort != "")
            {
                cmbxComPort.SelectedText = Properties.Settings.Default.COMPort;
            }
            chkAutoCut.Checked = Properties.Settings.Default.ScanImmediatelyCut;
            chkShowdialog.Checked = Properties.Settings.Default.ShowConfirmDialog;
        }

        private void btnCarlibration_Click(object sender, EventArgs e)
        {
            FrmSelectPrinter frm = new FrmSelectPrinter(cmbxComPort.SelectedIndex+1);
            frm.ShowDialog(this);
            if (CalibrationPrinterId != 0)
            {
                printerInfo = _bll.GetPrinterInfo(CalibrationPrinterId);
            }

        }

        private void btnControlCamera_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnControlCamera.Text == "显示相机")
                {
                    string ret = _bll.ShowCameraWindow(0,0, false);
                    if (!string.IsNullOrEmpty(ret))
                    {
                        MessageBox.Show(ret, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        btnControlCamera.Text = "隐藏相机";
                    }
                }
                else
                {
                    string ret = _bll.HideCameraWindow();
                    if (!string.IsNullOrEmpty(ret))
                    {
                        MessageBox.Show(ret, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        btnControlCamera.Text = "显示相机";
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                _bll.ResetMachine();
                MessageBox.Show("重置完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void cmbxComPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.COMPort = cmbxComPort.Text;
            Properties.Settings.Default.Save();
        }

        private void txtBoardNo_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyValue == (int)Keys.Enter)
            //{

            //    txtBoardNo.SelectAll();
            //}
        }


        private void FrmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            int temp =0;
            if (!txtBoardNo.Focused)
            {

                txtBoardNo.Text = e.KeyChar.ToString();

                txtBoardNo.SelectionLength = 0;
                txtBoardNo.SelectionStart = txtBoardNo.Text.Length;
                txtBoardNo.Focus();

            }
            if (e.KeyChar == (char)13)//按下回车键
            {
                if (chkAutoCut.Checked)
                {
                    btnCut.PerformClick();

                }
                txtBoardNo.SelectAll();
                rasterImg.Focus();

            }
            
        }

        private void chkAutoCut_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ScanImmediatelyCut = chkAutoCut.Checked;
            Properties.Settings.Default.Save();
        }

        private void chkShowdialog_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowConfirmDialog = chkShowdialog.Checked;
            Properties.Settings.Default.Save();
        }
    }
}
